<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>新增附件</title>
    <link rel="icon" th:href="@{/img/icon.ico}">
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/layuimini/css/public.css}" media="all">
    <style>
        .layui-form-item .layui-input-company {
            width: auto;padding-right: 10px;line-height: 38px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <form class="layui-form" action="" lay-filter="formDemo">
            <div class="layui-form layuimini-form">
                <div class="layui-form-item">
                    <label class="layui-form-label required">上传</label>
                    <div class="layui-input-block">
                        <button type="button" class="layui-btn" id="test1">
                            <i class="layui-icon">&#xe67c;</i>上传附件
                        </button>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" lay-verify="required" lay-reqtext="请输入附件名称" placeholder="请输入" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">路径</label>
                    <div class="layui-input-block">
                        <input type="text" name="url" lay-verify="required" lay-reqtext="请输入字典名称" placeholder="请输入" class="layui-input" readonly>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">后缀</label>
                    <div class="layui-input-block">
                        <input type="text" name="suffix" lay-verify="required" lay-reqtext="请输入字典名称" placeholder="请输入" class="layui-input" readonly>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn layui-btn-sm layui-btn-normal" lay-submit lay-filter="saveBtn" shiro:hasPermission="system:sys-files:add"> 保存 </button>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>

<script type="text/javascript" th:src="@{/js/jquery-3.2.0.min.js}"></script>
<script type="text/javascript" th:src="@{/layui/layui.js}"></script>
<script type="text/javascript" th:src="@{/js/common-table.js}"></script>

<script type="text/javascript" th:inline="javascript">

    layui.use(['form', 'upload', 'notice'], function () {
        var $ = layui.jquery,
            form = layui.form,
            notice = layui.notice,
            upload = layui.upload;

        var ctxPath = /*[[@{/}]]*/;
        var baseUrl = ctxPath + 'system/sys-files/';

        //执行实例
        var uploadInst = upload.render({
            elem: '#test1'
            ,url: baseUrl + '/upload/'
            ,accept: 'file'
            ,done: function(res){
                if(200 == res.code){
                    form.val("formDemo", res.data);
                    notice.success("上传成功");
                }else{
                    notice.error(res.msg);
                }
            }
            ,error: function(){
                notice.error("服务器异常");
            }
        });

        // 监听提交
        form.on('submit(saveBtn)', function (res) {
            var data = res.field;
            Asurplus.post({
                url: baseUrl + 'add',
                data: JSON.stringify(data)
            }, function () {
                setTimeout(function () {
                    var windowIndex = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(windowIndex);
                }, 1500)
            });
            return false;
        });
    });
</script>
</body>
</html>